
package com.sausageking.auth;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.sausageking.server.dao.GenericDao;
import com.sausageking.shared.Consumer;

import java.util.logging.Logger;

import javax.annotation.Nullable;

public class ConsumerDao {
  
  private static final  Logger logger = Logger.getLogger(ConsumerDao.class.getName());

  public boolean saveConsumer(Consumer consumer) {
    if (getConsumerByEmail(consumer.getEmail()) == null) {
      GenericDao<Consumer> dao = new GenericDao<Consumer>(Consumer.class);
      Key<Consumer> key = dao.put(consumer);
      logger.warning("key: " + key.getId() + "  " + key.getName() + "  "
          + key.getRaw());
      return true;
    } else {
      return false;
    }
  }

  /**
   * @return {@code null} if this email is not being used.
   */
  @Nullable
  public Consumer getConsumerByEmail(String email) {
    Consumer consumer = null;
    GenericDao<Consumer> dao = new GenericDao<Consumer>(Consumer.class);
    try {
      consumer = dao.get(email);
    } catch (NotFoundException ex) {
      return null;
    }
    return consumer;
  }
}